﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using Core.Domain;
using Library.Extensions.Expressions;

namespace Core.Queries
{   
    public class PolicyQuery : Query<Policy>
    {
        public int? Id {get;set;}
        public long? Number {get;set;}
        public string Name {get;set;}
        public string UserId { get; set; }
        public byte? StateId { get; set; }

        public override Expression<Func<Policy, bool>> ToExpression()
        {                                            
            Expression<Func<Policy, bool>> predicate = x => true;
            if (Id.HasValue)                            
                predicate = x => x.Id == Id;            
                            
            if (Number.HasValue)                            
                predicate = predicate.And(x => x.Number == Number);

            if (!string.IsNullOrWhiteSpace(Name))
                predicate = predicate.And(x => x.Person.Name.StartsWith(Name));

            if (!string.IsNullOrWhiteSpace(UserId))
                predicate = predicate.And(x => x.UserID.Equals(UserId));

            if(StateId.HasValue)
                predicate = predicate.And(x => x.StateID == StateId);

            return predicate;
        }       
    }
}
